package com.dld.coupon.activity;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CouponDetailScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.constant.net.Constant;
import com.dld.android.net.Param;
import com.dld.android.util.ReflectionFactory;
import com.dld.coupon.util.Common;
import com.dld.coupon.util.MapUtil;
import com.dld.protocol.image.ImageProtocol;
import com.dld.protocol.json.Protocol;
import com.json.entity.MyCouponDetailBean;

/**
 * http://shenzhen.dld.com/www/?r=www/api/discountDetail&userAgent=dld_android&disid=(int)
 * 
 * @author Administrator
 * 
 */
public class MyCouponDetail extends BaseActivity implements OnClickListener {

	private CouponDetailScrollView scroll;
	private Protocol ptl;
	private String address;

	private MyCouponDetailBean myDetailBean;
	private Handler mainhandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				bindViewByData((MyCouponDetailBean) msg.obj);
			} else if (msg.what == -1) {

			}
		}
	};

	private void bindViewByData(MyCouponDetailBean md) {
		address = md.getTitle();
		TextView cat = (TextView) findViewById(R.id.cat_name);
		TextView name = (TextView) findViewById(R.id.coupon_name);
		ImageView img = (ImageView) findViewById(R.id.image);
		TextView sdesp = (TextView) findViewById(R.id.short_desp);
		TextView orgprice = (TextView) findViewById(R.id.org_price);
		TextView discount = (TextView) findViewById(R.id.discount);
		TextView timeoff = (TextView) findViewById(R.id.time_off);
		TextView shopdes = (TextView) findViewById(R.id.shop_description);

		View imgmap = findViewById(R.id.map_view);

		img.setOnClickListener(this);
		cat.setText(md.getCatname());
		name.setText(md.getTitle());
		sdesp.setText(md.getShort_description());
		orgprice.setText(getString(R.string.yuanjia).toString()
				+ md.getCur_price() + getString(R.string.yuan).toString());
		discount.setText(md.getDiscount_lowest()
				+ getString(R.string.zhe).toString());
		timeoff.setText(md.timeOff());
		shopdes.setText(md.getShop_description());
		ImageProtocol imp = new ImageProtocol(this, md.getPreview_image());
		imp.startTrans(img);
		imgmap.setOnClickListener(this);

	}

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.coupon_detail_2012);
		this.scroll = ((CouponDetailScrollView) findViewById(R.id.scroll));
		this.scroll.setDrawingCacheEnabled(true);
		this.scroll.buildDrawingCache();
		init();
		initSegment();

	}

	private void init() {
		String disid = getIntent().getExtras().getString(
				Constant.CONSTANT_DISID);
		Param p = new Param();
		p.append("r", "www/api/discountDetail");
		p.append("userAgent", "dld_android");
		p.append("disid", disid);
		ptl = new Protocol(p);
		ptl.startTrans(new MyRetResult());

	}

	private class MyRetResult extends Protocol.OnJsonProtocolResult {

		public MyRetResult() {
			super(String.class);
		}

		@Override
		public void onException(String paramString, Exception paramException) {

		}

		@Override
		public void onResult(String paramString, Object paramObject) {
			String content = (String) paramObject;
			try {
				Element root = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder().parse(
								new ByteArrayInputStream(content.getBytes()))
						.getDocumentElement();
				MyCouponDetailBean md = ReflectionFactory.create(root
						.getElementsByTagName("result").item(0),
						MyCouponDetailBean.class);
				myDetailBean = md;
				int total = Integer.parseInt(root.getElementsByTagName("total")
						.item(0).getFirstChild().getNodeValue());
				if (total != 0) {
					mainhandler.sendMessage(Message.obtain(mainhandler, 0, md));
				} else {
					mainhandler.sendMessage(Message.obtain(mainhandler, -1));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public void onClick(View v) {
		if (v.getId() == R.id.image || v.getId() == R.id.map_view) {
			if (myDetailBean != null) {
				Common.startGoogleMap(v.getContext(), myDetailBean.getY(),
						myDetailBean.getX(),address);
			}
		}
	}

}
